#include "loginresponse.h"


#include <QVariant>


LoginResponse::LoginResponse(QObject *parent):
    JSONResponse(parent)
{
}

void LoginResponse::parse(const QVariant &data)
{
    if (data.type() != QVariant::Map){
        setError(Response::Property, "The field of error code is incompatible");
        return;
    }

    QVariantMap map = data.toMap();

    // Parse user id
    if (!map.contains("uid")) {
        setError(Response::Property, "The value of \"uid\" is missing");
        return;
    }

    QVariant userId = map.value("uid");

    // Parse userName
    if (!map.contains("urn")) {
        setError(Response::Property, "The value of \"urn\" is missing");
        return;
    }

    QVariant userName = map.value("urn");
    if (userName.type() != QVariant::String) {
        setError(Response::Property, "The value of \"urn\" is incompatible");
        return;
    }

    // Parse display Name
    if (!map.contains("dpn")) {
        setError(Response::Property, "The value of \"dpn\" is missing");
        return;
    }

    QVariant displayName = map.value("dpn");
    if (displayName.type() != QVariant::String) {
        setError(Response::Property, "The value of \"dpn\" is incompatible");
        return;
    }

    // Parse avatar url
    if (!map.contains("avatar")) {
        setError(Response::Property, "The value of \"avatar\" is missing");
        return;
    }

    QVariant avatar = map.value("avatar");
    if (avatar.type() != QVariant::String) {
        setError(Response::Property, "The value of \"avatar\" is incompatible");
        return;
    }

    // Parse session key
    if (!map.contains("skey")) {
        setError(Response::Property, "The value of \"skey\" is missing");
        return;
    }

    QVariant sessionKey = map.value("skey");
    if (sessionKey.type() != QVariant::String) {
        setError(Response::Property, "The value of \"skey\" is incompatible");
        return;
    }

    m_userId = userId.toLongLong();
    m_userName = userName.toString();
    m_displayName = displayName.toString();
    m_avatar = avatar.toString();
    m_sessionKey = sessionKey.toString();
}

